from django.db import models
from index.models import PetEncyclopediaModel
# Create your models here.
# 用户表
class UserModel(models.Model):
    mobile = models.CharField(max_length=20, verbose_name="手机号码")
    nikename = models.CharField(max_length=50, verbose_name="用户名")
    password = models.CharField(max_length=50, verbose_name="密码")
    email = models.EmailField(verbose_name="邮箱")
    pic_url = models.ImageField(upload_to='user/%Y/%m', verbose_name="用户头像")
    tscore = models.IntegerField(default=0, verbose_name="用户积分")
    addtime = models.DateTimeField(auto_now_add=True, verbose_name="添加时间")
    def __str__(self):
        return self.nikename
    class Meta:
        db_table = 'users'
        verbose_name_plural = '用户表'

# 三方登录表
class ThirdPartyLoginModel(models.Model):
    # 定义登录类型的选项
    LOGIN_TYPES = [
        ('dingding', '钉钉'),
        ('weixin', '微信'),
        ('qq', 'QQ'),
        # 可以继续添加其他登录类型
    ]

    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name="用户",related_name='third_party_logins')
    uid = models.CharField(max_length=50, unique=True, verbose_name="第三方UID")
    type = models.CharField(max_length=20, choices=LOGIN_TYPES, verbose_name="登录类型")
    def __str__(self):
        return self.uid
    class Meta:
        db_table = 'thirdpary'
        verbose_name_plural = '三方登录表'

# 收藏表
class CollectionModel(models.Model):
    user = models.ManyToManyField(UserModel, related_name='collections', verbose_name="用户ID")
    pet_encyclopedia = models.ManyToManyField(PetEncyclopediaModel, related_name='collections', verbose_name="文章ID")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    col_status = models.IntegerField(choices=(("1", "已收藏"), ("2", "取消收藏")), verbose_name="收藏状态",)

    def __str__(self):
        return f"用户{self.user.first().username}收藏了文章{self.pet_encyclopedia.first().title}"

    class Meta:
        db_table = "collection"
        verbose_name = "用户收藏表"
        verbose_name_plural = verbose_name


# 用户地址表
class UserAddressModel(models.Model):
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name="用户id")
    province = models.CharField(max_length=20, verbose_name="省份")
    city = models.CharField(max_length=20, verbose_name="城市")
    district = models.CharField(max_length=20, verbose_name="区县")
    detail = models.CharField(max_length=100, verbose_name="详细地址")
    linkman = models.CharField(max_length=50, verbose_name="联系人", blank=True, null=True)  # 可以为空
    mobile = models.CharField(max_length=11, verbose_name="手机号", blank=True, null=True)  # 可以为空

    def __str__(self):
        return f"{self.user.nikename}的地址"

    class Meta:
        db_table = "user_address"
        verbose_name = "用户地址表"
        verbose_name_plural = verbose_name